package com.thb.pay.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.thb.pay.api.payment.vo.req.ChannelConfigPageReq;
import com.thb.pay.api.payment.vo.req.ChannelConfigReq;
import com.thb.pay.api.payment.vo.resp.PayChannelVo;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.PayChannel;
import com.thb.pay.payment.mapper.PayChannelMapper;
import com.thb.pay.payment.service.PayChannelService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @Description 通道配置信息表 服务实现类
 * @Author lucky
 * @data 2019/9/26 22:34
 */
@Slf4j
@Service
public class PayChannelServiceImpl extends BaseServiceImpl<PayChannelMapper, PayChannel> implements PayChannelService {
    @Override
    public PageResult<PayChannelVo> findPage(ChannelConfigPageReq pageReq) {
        IPage<PayChannel> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<PayChannel> pageResult = super.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, PayChannelVo.class);
    }

    @Override
    public void add(ChannelConfigReq channelConfigReq) {
        Assert.notNull(channelConfigReq, "channelConfigReq.not.null");
        Assert.notNull(channelConfigReq.getChannelId(), "channelId.not.null");
        PayChannel payChannel = new PayChannel();
        BeanUtil.copyProperties(channelConfigReq, payChannel);
        super.insert(payChannel);
    }

    @Override
    public void update(ChannelConfigReq channelConfigReq) {
        Assert.notNull(channelConfigReq, "channelConfigReq.not.null");
        Assert.notNull(channelConfigReq.getId(), "channelConfigId.not.null");
        PayChannel payChannel = super.selectById(channelConfigReq.getId());
        Assert.notNull(payChannel, "payChannel.not.null");
        PayChannel targetPayChannel = new PayChannel();
        BeanUtil.copyProperties(channelConfigReq, targetPayChannel);
        super.updateById(targetPayChannel);
    }

    @Override
    public PayChannelVo getById(Long id) {
        Assert.notNull(id, "channelConfigId.not.null");
        PayChannel payChannel = super.selectById(id);
        if (payChannel != null) {
            PayChannelVo payChannelVo = new PayChannelVo();
            BeanUtil.copyProperties(payChannel, payChannelVo);
            return payChannelVo;
        }
        return null;
    }

    @Override
    public PayChannelVo getByChannelCode(String channelCode) {
        Assert.notNull(channelCode, "channelCode.not.null");
        LambdaQueryWrapper<PayChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PayChannel::getCode, channelCode);
        List<PayChannel> payChannelList = super.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(payChannelList)) {
            PayChannelVo payChannelVo = new PayChannelVo();
            BeanUtil.copyProperties(payChannelList.get(0), payChannelVo);
            return payChannelVo;
        }
        return null;
    }
}
